/**
 * 发货
 * @param tid
 * 			淘宝的订单编号
 */
function sendGoods() {
	$.getJSON('./taobao/sendGoods.action', $( "#form" ).serialize(), function(data) {
		var str = data.context + "\n";
		
		for(var t in data.data) {
			str += data.data[t] + "\n";
		}
		alert(str);
	});
}

function init() {
	// 设置基本的信息
	$(" #buyyer_name ").empty();
	$(" #buyyer_address ").empty();
	
	// 填写买家基本信息
    $.ajax({
    	url: "./taobao/getTradeSimpleInfo.action",
    	type: "GET",
    	data: { tid: $("#tid").val() },
    	dataType: "json"
    	}).done(function(data) {
    		$(" #buyyer_name ").append("买家ID: " + data.buyyer);
    		$(" #buyyer_address ").append("买家地址: " + data.address);
    	}).fail(function() { alert("error"); });
    	
    // 填写运单号
    $.ajax({
    	url: "./taobao/getOutSidFromIni.action",
    	type: "GET",
    	data: { expressCode: $( "#expressName" ).val() },
    	dataType: "text"
    	}).done(function(data) {
    		var value = new Number(data) + 1;
    		$( "#out_sid" ).val(formatOutSid(value, $( "#expressName" ).val()));
    	}).fail(function() { alert("error"); });
    	
    	$( "#out_sid ").keypress(function() {
    		var value = $.trim(filterSpace($( "#out_sid" ).val()));
    		$( "#out_sid" ).val(formatOutSid(value, $( "#expressName" ).val()));
    	});
	
	$('#out_sid').keydown(function(event) {
  		var num = Number(filterSpace($( "#out_sid" ).val()));
  		var keycode = event.which;
  		
  		if((keycode >=48 && keycode <= 57) || (keycode >=96 && keycode <= 105)) {
  		} else if(keycode == 38 || keycode == 39){
  			$( "#out_sid" ).val(formatOutSid(num + 1, $( "#expressName" ).val()));
  		} else if(keycode == 37 || keycode == 40){
  			$( "#out_sid" ).val(formatOutSid(num - 1, $( "#expressName" ).val()));
  		} else if(keycode >= 65 && keycode <= 90) {
  			event.returnvalue = false;
  		}
	});
}

/**
 * 过滤字符串中间的空格
 * @param str
 * 			要过滤的字符串
 * @returns
 * 			过滤好的字符串
 */
function filterSpace(str) {
	var array = str.split(" ");
	
	var str = "";
	for(var i in array) {
		str += array[i];
	}
	return $.trim(str);
}

/**
 * 格式化运单号
 * @param outSid
 * 			运单号
 * @param express
 * 			快递公司
 * @returns
 * 			格式化后的运单号
 */
function formatOutSid(outSid, express) {
	outSid = new String(outSid);
	
	switch (express) {
		case "中通速递":   				
		case "顺丰速运":   				
		case "顺丰速运(香港)":   				
		case "全峰快递":   				
		case "申通E物流":
			outSid = outSid.replace(/(\d{3})/g, "$1 ");
			break;
		case "圆通":
		case "宅急送":
			break;
	}
	
	return $.trim(outSid);
}

/**
 * 运单号的输入事件(输入运单号时进行格式化)
 */
function outSidKeypress() {
	var value = $.trim(filterSpace($( "#out_sid" ).val()));
	$( "#out_sid" ).val(formatOutSid(value, $( "#express" ).val()));
}